package shuishijie.suishijie.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by zhangrl on 2017/8/30.
 */
public abstract class BasePopup<T> extends PopupWindow implements View.OnClickListener {
    Activity activity;
    View rootView;
    View parent;
    T data;

    public BasePopup(Activity activity, int LayoutId, T data) {
        super();
        rootView = LayoutInflater.from(activity).inflate(LayoutId, null);
        setContentView(rootView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.data = data;
        this.activity = activity;
        this.update();
        this.setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x01ffffff));
        setFocusable(true);

        initView(data);
    }

    protected abstract void initView(T data);

    public void show(View parent, int gravity, int x, int y) {
        showAtLocation(parent, gravity, x, y);
        setWindowAlpha(0.5f);
        this.parent = parent;
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        setWindowAlpha(0.5f);
        this.parent = anchor;
    }

    public <V> V getView(int id) {
        return (V) rootView.findViewById(id);
    }

    public BasePopup<T> setText(int id, String value) {
        TextView view = getView(id);
        if (view != null) {
            view.setText(value);
        }
        return this;
    }

    public BasePopup<T> addOnClickView(int id) {
        View view = getView(id);
        if (view != null) {
            view.setOnClickListener(this);
        }
        return this;
    }

    View.OnClickListener l;

    public BasePopup<T> setOnClickListener(View.OnClickListener l) {
        this.l = l;
        return this;
    }


    @Override
    public void onClick(View v) {
        if (l != null) {
            l.onClick(v);
        }
    }


    private void setWindowAlpha(float alpha) {
        Window window = activity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = alpha;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setAttributes(lp);
        super.setOnDismissListener(onDismissListener);
    }

    OnDismissListener listener;

    @Override
    public void setOnDismissListener(OnDismissListener onDismissListener) {
        this.listener = onDismissListener;
    }

    private OnDismissListener onDismissListener = new OnDismissListener() {
        @Override
        public void onDismiss() {
            setWindowAlpha(1f);
            if (listener != null) {
                listener.onDismiss();
            }
        }
    };


    Toast toast = null;

    public void showMessage(String msg) {
        if (activity == null) {
            return;
        }
        if (toast == null) {
            toast = Toast.makeText(activity, msg, Toast.LENGTH_SHORT);
        }
        toast.setText(msg);
        toast.show();
    }

    public void hideMessage() {
        if (toast != null) {
            toast.cancel();
        }
    }
}
